﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.NamedEntities
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class OrganizationEncodingOutput:ResultBase
    {
        #region props
        private string _OutputTable;
        /// <summary></summary>
        public string OutputTable
        {
            get
            {
                return this._OutputTable;
            }
            set
            {
                this._OutputTable = value;
            }
        }

        private int _OrgEntryCount;
        /// <summary></summary>
        public int OrgEntryCount
        {
            get
            {
                return this._OrgEntryCount;
            }
            set
            {
                this._OrgEntryCount = value;
            }
        }

        private int _DistinctOrgCount;
        /// <summary></summary>
        public int DistinctOrgCount
        {
            get
            {
                return this._DistinctOrgCount;
            }
            set
            {
                this._DistinctOrgCount = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public OrganizationEncodingOutput():base(Guid.Empty,Guid.Empty)
        {
            this._OutputTable = string.Empty;
            this._OrgEntryCount = 0;
            this._DistinctOrgCount = 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="outputTableName"></param>
        /// <param name="orgEntryCount"></param>
        /// <param name="distOrgCount"></param>
        public OrganizationEncodingOutput(Guid componentID, Guid jobTicketID,
            string outputTableName, int orgEntryCount, int distOrgCount) : base(componentID, jobTicketID)
        {
            this._OutputTable = outputTableName;
            this._OrgEntryCount = orgEntryCount;
            this._DistinctOrgCount = distOrgCount;
        }
        #endregion

        #region Overrides of ResultBase

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="parentNode"></param>
        /// <returns></returns>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "OutputTable", this.OutputTable);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "OrgEntryCount", this.OrgEntryCount.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "DistinctOrgCount", this.DistinctOrgCount.ToString());

            return xNode;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xNode"></param>
        /// <returns></returns>
        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                OrganizationEncodingOutput output = new OrganizationEncodingOutput();
                output.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                output.FinishTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FinishTime").Value);

                output.OutputTable =
                    XmlDataUtil.GetAttributeValue(xNode, "OutputTable", "");
                output.OrgEntryCount =
                    int.Parse(XmlDataUtil.GetAttributeValue(xNode, "OrgEntryCount", "0"));
                output.DistinctOrgCount =
                    int.Parse(XmlDataUtil.GetAttributeValue(xNode, "DistinctOrgCount", "0"));

                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }

        #endregion
    }
}
